package com.sinosoft.web.easyui.api;

import com.sinosoft.hydra.logging.LogModules;
import cn.com.sinux.spring.commons.PropertyCollection;
import com.sinosoft.hydra.um.condition.SearchLogPackageCondition;
import com.sinosoft.hydra.um.domain.permission.SysLogPackage;
import com.sinosoft.hydra.um.helper.LoggerHelper;
import com.sinosoft.hydra.um.services.SysLogPackageService;
import cn.com.sinux.spring.utils.PageUtils;
import cn.com.sinux.spring.json.JsonFilter;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>日志打包管理控制层.</p>
 *
 * @author oujianlin@sinosoft.co
 * @author chendlqueryPackage
 * @version 0.5-alpha
 * @since 0.5
 */
@Controller
@RequestMapping(value = "/api/logPackage")
public class SysLogPackageApi {
    /**
     * 调用日志打包的service层SysLogPackageService
     */
    @Autowired
    private SysLogPackageService sysLogPackageService;

    /**
     * <p>查询日志打包列表.</p>
     *
     * @param searchLogPackageCondition 日志打包对象（搜索内容）
     * @param pageSize                  每页大小
     * @param pageNumber                页数
     * @return 返回页面信息JSON格式
     */
    @RequestMapping(value = "/queryPageForJson")
    @ResponseBody
    public Object queryPageForJson(@RequestParam(value = "rows", defaultValue = "10", required = false) Integer pageSize,
                                   @RequestParam(value = "page", defaultValue = "1", required = false) Integer pageNumber,
                                   SearchLogPackageCondition searchLogPackageCondition) {
        Pageable pageable = PageUtils.buildPageRequest(pageNumber, pageSize, "createDate", "desc");
        Page<SysLogPackage> page = sysLogPackageService.findAllByPage(pageable, searchLogPackageCondition);
        return page;
    }

    /**
     * <p>初始化日志打包列表页面.</p>
     *
     * @param model 将返回页面信息放入model
     * @return 返回初始化页面
     */
    @RequestMapping(value = "/queryPackage")
    @RequiresPermissions(value = "log:package:list")
    public String queryOrganizationsForSubmit(Model model) {
        return "hydra_easyui/logPackage/index";
    }

    /**
     * <p>初始化日志打包列表页面.</p>
     *
     * @param model 将返回页面信息放入model
     * @return 返回初始化页面
     */
    @RequestMapping(value = "/search")
    @RequiresPermissions(value = "log:package:list")
    public String search(Model model) {
        return "hydra_easyui/logPackage/search";
    }

    /**
     * 根据传递的 ID集合批量删除打包日志
     *
     * @param searchLogPackageCondition Id集合({"ids":["1","2","3"]})
     * @param model                     将返回页面信息放入model
     * @return 返回删除结果
     */
    @ResponseBody
    @RequestMapping(value = "/deletePackage", method = RequestMethod.POST)
    @RequiresPermissions(value = "log:package:delete")
    public Object deletePackage(SearchLogPackageCondition searchLogPackageCondition, Model model) {
        List<SysLogPackage> logPackages = sysLogPackageService.deleteLogPackage(searchLogPackageCondition.getIds());
        LoggerFactory.getLogger(LogModules.TRACE_LOGGER).info("删除了日志打包信息{}",
                new LoggerHelper.PropertyReader<SysLogPackage>(logPackages, "packageName"));
        return JsonFilter.doPropertyFilter(logPackages, new PropertyCollection()
                .addProperties(SysLogPackage.class, "id"));
    }
}
